package com.hunttown.mes.service;

import com.hunttown.mes.common.utils.FastJsonUtils;
import com.hunttown.mes.common.utils.GeneralUtils;
import com.hunttown.mes.common.utils.ScriptFilterUtils;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.dao.TransportRolesDao;
import com.hunttown.mes.domain.TransportRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * created by wangjunfu on 2022-01-28 11:17:35
 */
@Service
public class TransportRolesService {

    private final static Logger logger = LoggerFactory.getLogger(TransportRolesService.class);

    private final TransportRolesDao transportRolesDao;
    private final AHelpService helpService;

    @Autowired
    public TransportRolesService(TransportRolesDao transportRolesDao, AHelpService helpService) {
        this.transportRolesDao = transportRolesDao;
        this.helpService = helpService;
    }

    /**
     * 新增
     *
     * @param transportRoles 实体对象
     * @return 最新插入id
     */
    public TransportRoles insert(TransportRoles transportRoles) {
        //api接口传进来是有值的
        if (transportRoles.getEntId() == null)
            transportRoles.setEntId(helpService.getEnterpriseId());
        if (transportRoles.getAdminId() == null)
            transportRoles.setAdminId(0);
        if (transportRoles.getCreateTime() == null)
            transportRoles.setCreateTime(new Date());
        if (transportRoles.getStopFlag() == null)
            transportRoles.setStopFlag(0);
        if (transportRoles.getDeleteFlag() == null)
            transportRoles.setDeleteFlag(0);

        if (ScriptFilterUtils.xssFilterForDomain(GeneralUtils.ergodicModel(FastJsonUtils.toJSONString(transportRoles), TransportRoles.class))) {
            return transportRoles;
        }
        return transportRolesDao.insert(transportRoles);
    }

    /**
     * 根据ID修改数据信息
     *
     * @param transportRoles 实体对象
     * @return True or False
     */
    public Boolean updateInfoById(TransportRoles transportRoles) {
        if (transportRoles.getEntId() == null) {
            transportRoles.setEntId(helpService.getEnterpriseId());
        }

        if (ScriptFilterUtils.xssFilterForDomain(GeneralUtils.ergodicModel(FastJsonUtils.toJSONString(transportRoles), TransportRoles.class))) {
            return false;
        }
        return transportRolesDao.updateInfoById(transportRoles);
    }

    /**
     * 根据Query修改数据信息
     *
     * @param query 需要修改的字段 KV 格式
     * @return True or False
     */
    public Boolean updateInfoByQuery(Query query) {
        if (!query.containsKey("w_entId")) {
            query.put("w_entId", helpService.getEnterpriseId());
        }

        if (ScriptFilterUtils.xssFilterForQuery(query)) {
            return false;
        }
        return transportRolesDao.updateInfoByQuery(query);
    }

    /**
     * 根据ID获取记录信息
     *
     * @param id 主键id
     * @return 实体对象
     */
    public TransportRoles selectById(Integer id) {
        return transportRolesDao.selectById(id, helpService.getEnterpriseId());
    }

    /**
     * 根据query取出一条记录，但是可能有多条记录，取最新的一条。
     *
     * @param query 查询条件 KV 格式
     * @return 实体对象
     */
    public TransportRoles selectObjByQuery(Query query) {
        if (query == null || query.size() == 0) {
            return null;
        }
        if (!query.containsKey("entId")) {
            query.put("entId", helpService.getEnterpriseId());
        }

        List<TransportRoles> list = selectByQuery(query);
        if (list == null || list.size() == 0) {
            return null;
        }

        return list.get(0);
    }

    /**
     * 根据条件获取List
     *
     * @param query 查询条件 KV 格式
     * @return 实体对象列表
     */
    public List<TransportRoles> selectByQuery(Query query) {
        if (!query.containsKey("deleteFlag")) {
            query.put("deleteFlag", 0);
        }
        if (!query.containsKey("entId")) {
            query.put("entId", helpService.getEnterpriseId());
        }

        return transportRolesDao.selectByQuery(query);
    }

    /**
     * 根据条件获取记录总数
     *
     * @param query 查询条件 KV 格式
     * @return 总记录数
     */
    public Long countByQuery(Query query) {
        if (!query.containsKey("deleteFlag")) {
            query.put("deleteFlag", 0);
        }
        if (!query.containsKey("entId")) {
            query.put("entId", helpService.getEnterpriseId());
        }

        return transportRolesDao.countByQuery(query);
    }

    /**
     * 根据ID删除记录信息
     *
     * @param id 主键id
     * @return True or False
     */
    public Boolean deleteById(Integer id) {
        return transportRolesDao.deleteById(id, helpService.getEnterpriseId());
    }
}